﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace colasContinua.Metodos_numericos
{
    public class rungeKutta
    {
        public static double calcularTiempo(double rnd)
        {
            double alfa = Math.Log(100) / 100;
            double h = 0.0001;
            double t = 0;
            double T = 1;
            double k1 = 0;

            double TiMasHSobreDosPork1 = 0;
            double k2 = 0;
            double TiMasHSobreDosPork2 = 0;
            double k3 = 0;

            double TiMasHPork3;
            double k4 = 0;

            if (rnd < 0.5)
            {
                while (T < 100)
                {
                    t = t + h;
                    k1 = alfa * T;
                    TiMasHSobreDosPork1 = T + ((h / 2) * k1);
                    k2 = alfa * TiMasHSobreDosPork1;
                    TiMasHSobreDosPork2 = T + ((h / 2) * k2);
                    k3 = alfa * TiMasHSobreDosPork2;
                    TiMasHPork3 = T + (h * k3);
                    k4 = alfa * TiMasHPork3;
                    T = T + ((h / 6) * (k1 + (2 * k2) + (2 * k3) + k4));
                }
            }
            else if (rnd >= 0.5 && rnd < 0.8)
            {
                while (T < 70)
                {
                    t = t + h;
                    k1 = alfa * T;
                    TiMasHSobreDosPork1 = T + ((h / 2) * k1);
                    k2 = alfa * TiMasHSobreDosPork1;
                    TiMasHSobreDosPork2 = T + ((h / 2) * k2);
                    k3 = alfa * TiMasHSobreDosPork2;
                    TiMasHPork3 = T + (h * k3);
                    k4 = alfa * TiMasHPork3;
                    T = T + ((h / 6) * (k1 + (2 * k2) + (2 * k3) + k4));
                }
            }
            else
            {
                while (T < 50)
                {
                    t = t + h;
                    k1 = alfa * T;
                    TiMasHSobreDosPork1 = T + ((h / 2) * k1);
                    k2 = alfa * TiMasHSobreDosPork1;
                    TiMasHSobreDosPork2 = T + ((h / 2) * k2);
                    k3 = alfa * TiMasHSobreDosPork2;
                    TiMasHPork3 = T + (h * k3);
                    k4 = alfa * TiMasHPork3;
                    T = T + ((h / 6) * (k1 + (2 * k2) + (2 * k3) + k4));
                }
            }
            return t;
        }
    }
}
